import {Component} from "@angular/core";
import {NavController, Refresher, ToastController} from "ionic-angular";
import {HomeService} from "./home.service";
import {LoginPage} from "../login/login";
import {UserData} from "../../common/user.data";
import {PublishPage} from "../publish/publish";
import {SubjectPage} from "../subject/subject";
import {ArticlePage} from "../article/article";
import {WebViewPage} from "../webview/webview";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers: [HomeService],
})
export class HomePage {

  private articleTab: string = "recommend";

  private commendArticle: any = {
    commendLoadOver: false,
    recommendArticles: [],
    page: 0,
    size: 20
  };
  private article: any = {
    loadOver: false,
    articles: [],
    page: 0,
    size: 20
  };

  constructor(private navController: NavController,
              private toastCtrl: ToastController,
              private user: UserData,
              private homeService: HomeService,) {
  }

  ionViewDidLoad() {
    this.updateRecommendArticle();
    this.updateArticle();
  }


  private updateRecommendArticle() {
    this.homeService.findRecommendArticle().subscribe(data => {
      this.commendArticle.recommendArticles = data.data.content;
    });
  }

  recommendArticleRefresh(refresher: Refresher) {
    this.homeService.findRecommendArticle().subscribe(data => {
      this.commendArticle.recommendArticles = data.data.content;
      this.commendArticle.commendLoadOver = false;
      setTimeout(() => {
        refresher.complete();
      }, 1000);
    }, () => {
      refresher.complete()
    });
  }
  webview(){
    this.navController.push(WebViewPage)
  }

  loadRecommendMore(infiniteScroll: any) {
    if (this.commendArticle.commendLoadOver) {
      infiniteScroll.complete();
      return;
    }
    this.commendArticle.page += 1;
    this.homeService.findRecommendArticle({page: this.commendArticle.page}).subscribe(res => {
      if (res.status === "SUCCESS") {
        if (res.data.content.length < this.commendArticle.size) {
          this.commendArticle.commendLoadOver = true;
        }
        if (this.commendArticle.recommendArticles) {
          for (let v in res.data.content) {
            this.commendArticle.recommendArticles.push(res.data.content[v]);
          }
        } else {
          this.commendArticle.recommendArticles = res.data.content;
        }
        if (infiniteScroll)
          infiniteScroll.complete();
      } else {
        this.toastCtrl.create({
          message: res.message,
          duration: 2000,
          position: 'top'
        }).present();
        if (infiniteScroll)
          infiniteScroll.complete();
      }
    })
  }

  loadArticleMore(infiniteScroll: any) {
    if (this.article.loadOver) {
      infiniteScroll.complete();
      return;
    }
    this.article.page += 1;
    this.homeService.findArticle({page: this.article.page}).subscribe(res => {
      if (res.status === "SUCCESS") {
        if (res.data.content.length < this.article.size) {
          this.article.loadOver = true;
        }
        if (this.article.articles) {
          for (let v in res.data.content) {
            this.article.articles.push(res.data.content[v]);
          }
        } else {
          this.article.articles = res.data.content;
        }
        if (infiniteScroll)
          infiniteScroll.complete();
      } else {
        this.toastCtrl.create({
          message: res.message,
          duration: 2000,
          position: 'top'
        }).present();
        if (infiniteScroll)
          infiniteScroll.complete();
      }
    })
  }

  updateArticle() {
    this.homeService.findArticle().subscribe(data => {
      this.article.articles = data.data.content;
    });
  }

  publishArticleClick() {
    this.user.token ? this.navController.push(PublishPage) : this.navController.push(LoginPage);
  }

  createSubjectClick() {
    this.user.token ? this.navController.push(SubjectPage) : this.navController.push(LoginPage);
  }

  articleDetailClick(article: any) {
    this.navController.push(ArticlePage, {article: article})
  }

  articleRefresh(refresher: Refresher) {
    this.homeService.findArticle().subscribe(data => {
      this.article.articles = data.data.content;
      this.article.loadOver = false;
      setTimeout(() => {
        refresher.complete();
      }, 1000);
    }, () => {
      refresher.complete()
    });
  }
}
